var mta = require('common/mta_analysis.js')
App({
  // 正式平台217服务器 
  serviceurl: "https://app.hn96606.com/",
  // 测试平台
  // serviceurl: "https://wxm.hn96606.com/",
  //  serviceurl: "http://10.0.5.215:8086/",
  //  serviceurl: "http://172.16.101.80:8096/",
  // 主题色
  appColor: "background-color:#01a1e9;",
  // 服务器217上的html连接
  webHtmlData: {},
  // banner图
  appimg: [],
  globalcode: "",
  stationName: "",
  target: "",
  businfo: "",
  notice: '',
  takemaninfo: "",
  IsRegister: false,
  line: {
    TakeManID: []
  },
  isbao: "",
  userinfo: {
    headimg: '',
    nickname: '',
  },
  customline: {
    linename: '',
    linedate: ''
  },
  globalBaoXian: {
    ProductId: '',
    IsInsure: ''
  },
  accesstoken: false,
  // 备用token
  stanToken: false,
  // 分享来源
  optionsToken: "",
  count: 0,
  hint: false,
  busHint: false,
  // carHint: false,
  busHintRepetition: false,
  activeIndex: 0,
  deviceinfo: '',
  IsScanCOde: false,
  // 存储用户信息，在index页面请求Token
  o: {},
  optionsP: "",
  // 优惠ID
  reciveId: "",
  // 优惠金额
  recivePrice: false,
  // 车站是否支持在线退票
  isRefund: false,
  // 判断是否是新用户
  newUser: false,
  // 是否显示分享有礼
  isGift: false,
  // 是否为新用户
  IsFresh: false,
  // 网络数据
  networkData: "",
  // 用户手机型号
  appmodel: false,
  onLaunch: function(o) {
    this.appimg = [
      `https://img.hn96606.com/images/banner1.jpg?${this.dates()}`,
      `https://img.hn96606.com/images/banner2.png?${this.dates()}`
    ];
    // 网络数据，便于热更新数据，而不需要重新提交小程序
    this.networking();
    // 获取用户Token
    this.o = o;
    // this.dates();
    this.tongji(o);
    this.getSystemInfo();
    this.onloadToken();
  },
  // 各种手机适配
  numHeight() {
    let windowHeight = Math.abs(1284 - wx.getSystemInfoSync().windowHeight * 2);
    // iphoneX
    if (windowHeight == 0) {
      return 274
    }
    // iphone Plus 5.5 || Iphone XR XS MAX
    if (windowHeight == 36 || windowHeight == 168) {
      return 264
    }
    // 小屏5S
    if (windowHeight >= 365 & windowHeight <= 400 ) {
      return 270
    }
    // ipad大屏
    if (windowHeight >= 520 & windowHeight <= 920) {
      return 245
    }
    // android ??
    if (windowHeight >= 30 & windowHeight <= 80) {
      return 262
    }
    // android ??寸
    if (windowHeight >= 80 & windowHeight <= 120) {
      return 278
    }
    if (windowHeight >= 120 & windowHeight <= 180) {
      return 260
    }
    // 中屏
    if (windowHeight >= 260 & windowHeight <= 365) {
      return 268
    } else {
      return 270
    }
  },
  // 每次进入小程序都会触发此函数，onLoad函数则在首次编译加载时才会出发！！！
  onShow: function() {

  },
  onHide: function() {},

  networking: function() {
    let that = this;
    wx.request({
      url: 'https://img.hn96606.com/images/networkData.json',
      headers: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
        that.networkData = res.data;
        // if(res.data.dis == true){
        //   that.appimg.unshift(`https://img.hn96606.com/images/bannerDis.png?${that.dates()}`)
        // }
      }
    })
  },
  onError: function(msg) {
    try {
      wx.request({
        url: this.serviceurl + 'CollectError',
        data: {
          DeviceInfo: this.deviceinfo,
          ErrorMsg: msg
        },
        dataType: 'json',
        success: function(res) {

        },
        fail: function() {}
      });
    } catch (e) {}
  },
  // 判断是否获取到Token，如果没有获取到新Token则先使用老Token
  onloadToken: function() {
    let that = this;
    wx.getStorage({
      key: '_userToken',
      success(res) {
        if (res.data.accesstoken) {
          that.stanToken = res.data.accesstoken;
        }
      },
      fail(err) {}
    })
  },
  globalData: {
    hasLogin: false
  },
  getSystemInfo: function() {
    var that = this;
    try {
      wx.getSystemInfo({
        success: function(res) {
          that.deviceinfo = res;
        }
      })
    } catch (e) {}
  },
  showLoadings: function() {
    try {
      if (wx.showLoading) {
        wx.showLoading({
          title: '加载中',
        })
      }
    } catch (e) {}
  },

  hideLoadings: function() {
    try {
      if (wx.hideLoading) {
        wx.hideLoading();
      }
    } catch (e) {}
  },

  tongji: function(options) {
    try {
      mta.App.init({
        "appID": "500496952",
        "lauchOpts": options,
      });
    } catch (e) {}
  },
  // 设置日期(测试)
  dates: function(date) {
    var d = new Date(),
      month = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1,
      day = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();
    this.startdate = d.getFullYear() + "-" + month + "-" + day;
    return d.getFullYear() + "-" + month + "-" + day;
  },
  // 2020春节活动活动详情弹框每天显示一次
  disDate: function (date) {
    var d = new Date(),
      month = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1,
      day = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();
    this.startdate = d.getFullYear() + "-" + month + "-" + day;
    return `${d.getFullYear()}${month}${day}`;
  },

  OnShare: function(msg) {
    var sharepath = "/pages/index/index?user=" + this.accesstoken;
    return {
      title: '豫州行',
      path: sharepath,
      success: function(res) {
        // 转发成功
      },
      fail: function(res) {
        // 转发失败 
      }
    }
  },
  // 2020春节优惠活动分享/===调用该分享时图片可能会加载不出来
  disShare: function(){
    return {
      title: "",
      path: `/pages/index/index?user=${this.accesstoken}&dis=true&shareid=${new Date().getTime()}`,
      imageUrl: 'https://img.hn96606.com/images/activity/fenxiang.png',
      success: function (res) {

      },
      fail(err) {

      }
    }
  }
});